package com.cms.freemark;

import java.io.IOException;
import java.util.Map;

import org.springframework.stereotype.Component;

import com.cms.common.StringUtils;

import freemarker.core.Environment;
import freemarker.template.TemplateDirectiveBody;
import freemarker.template.TemplateDirectiveModel;
import freemarker.template.TemplateException;
import freemarker.template.TemplateModel;

@Component(value="textCutDirectiveModel")
public class TextCutDirectiveModel implements TemplateDirectiveModel{

	@Override
	public void execute(Environment event, Map map, TemplateModel[] model,
			TemplateDirectiveBody body) throws TemplateException, IOException {
		StringBuilder sb = new StringBuilder();
		String value = StringUtils.obj2String(map.get("value"));
		int length = StringUtils.str2Int(map.get("len")+"");
		if (length==0) {
			length=15;
		}
		if(value.length()>length)
		{
			sb.append(StringUtils.substring(value, 0, length));
			sb.append(".....");
		}
		else
		{
			sb.append(value);
		}
	    event.getOut().write(sb.toString());
	    event.getOut().flush();
	}
	
	
	public static void main(String[] args) {
		String value ="08,01,03,09,04,02,05,06,10,07";
		StringBuilder sb = new StringBuilder();
		int length=15;
		sb.append(StringUtils.substring(value, 0, length));
		if(length>=15)
		{
			sb.append(".....");
		}
		System.out.println(sb.toString());
		
	}

}
